$(document).ready(function() {
	var curPage = $("input[name=page]").val();
	var pageSize = $("input[name=rows]").val();
	var totalCount = $("#totalValue").val();
	$('ul#pagination').page({
		"curPage": parseInt(curPage),
		"pageSize": parseInt(pageSize),
		"totalCount": parseInt(totalCount),
		"init": true,
		"callback": function (page) {
			query(page);
		}
	});

	initSelect2();

	//开始日期控件
	$('input[name="createTimeBegin"]').datetimepicker({
		format : 'yyyy-mm-dd',
		weekStart : 1,
		todayBtn : 1,
		autoclose : 1,
		todayHighlight : 1,
		startView : 2,
		minView : 2,
		forceParse : true,
		language : 'zh-CN',
		clearBtn : true
	}).on("click",function(ev) {
		$('input[name="createTimeBegin"]').datetimepicker("setEndDate", $('input[name="createTimeEnd"]').val());
	});

	//结束日期控件
	$('input[name="createTimeEnd"]').datetimepicker({
		format : 'yyyy-mm-dd',
		weekStart : 1,
		todayBtn : 1,
		autoclose : 1,
		todayHighlight : 1,
		startView : 2,
		minView : 2,
		forceParse : true,
		language : 'zh-CN',
		clearBtn : true
	}).on("click",function(ev){
		$('input[name="createTimeEnd"]').datetimepicker("setStartDate", $('input[name="createTimeBegin"]').val());
	});
});

function query(cp) {
    var userId = $("select[name='userId']").select2('data');
    if (userId.length != 0) {
        $('#queryForm input:hidden[name=userIdName]').val(userId[0].text);
    }
	$('#queryForm input:hidden[name=page]').val(cp);
	$('#queryForm input:hidden[name=rows]').val(10);
	$('#queryForm').submit();
}

//查询按钮
$('#btnQuery').on({
	  click: function() {
		  query(1);
	  }
});

//重置按钮
$('#btnReset').on({
	  click: function() {
		  $('#queryForm input,select').not('input:hidden[name=page],input:hidden[name=rows]').val('');
		  $("select[name='userId']").empty();
		  initSelect2();
	  }
});

//导出按钮
$('#btnExport').on({
	click: function() {
		var userId = $("#queryForm select[name='userId']").val();
		if (null != userId) {
			$("#exportForm input[name='userId']").val(userId);
		}

		var address = $("#queryForm input[name='address']").val();
		if (null != address) {
			$("#exportForm input[name='address']").val(address);
		}

		var createTimeBeginStr = $("#queryForm input[name='createTimeBeginStr']").val();
		if (null != createTimeBeginStr) {
			$("#exportForm input[name='createTimeBeginStr']").val(createTimeBeginStr);
		}

		var createTimeEndStr = $("#queryForm input[name='createTimeEndStr']").val();
		if (null != createTimeEndStr) {
			$("#exportForm input[name='createTimeEndStr']").val(createTimeEndStr);
		}
		$("#exportForm").submit();
	}
});

function initSelect2(){
	$("select[name='userId']").select2({
		language : "zh-CN",
		placeholder : '请选择',
		allowClear : true,
		minimumInputLength : 1,
		ajax : {
			type: "POST",
			url : "/user/data",
			dataType : 'json',
			delay : 250,
			data : function(params) {
				return {
					qLoginIdOrUserName : params.term,
					page : params.page ? params.page : 1,
					rows : 10
				};
			},
			processResults : function(data, params) {
				var results = [];
				$.each(data.rows, function(i, v) {
					var o = {};
					o.id = v.id;
					o.text = v.showName;
					o.deptName = v.deptName;
					results.push(o);
				})

				params.page = params.page || 1;
				return {
					results : results,
					pagination : {
						more : (params.page * 10) < data.total
					}
				};
			},
			cache : false
		},
		escapeMarkup: function (markup) { return markup; },
		templateResult: formatUserRepo,
		templateSelection: formatUserRepoSelection
	});
}